package com.xnx3.wangmarket.plugin.tencentYinShipin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tencentyun.TLSSigAPIv2;
import com.xnx3.Lang;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.service.SmsService;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.CacheUtil;
import com.xnx3.kefu.core.bean.ChatUserBean;
import com.xnx3.kefu.core.util.TokenUtil;
import com.xnx3.kefu.core.util.UserUtil;
import com.xnx3.wangmarket.plugin.tencentYinShipin.Global;
import com.xnx3.wangmarket.plugin.tencentYinShipin.vo.UserSigVO;

/**
 * 腾讯云音视频
 * @author 管雷鸣
 */
@Controller(value="TencentYinShipinKefuPluginController")
@RequestMapping("/plugin/tencentYinShipin/")
public class TencentYinShipinPluginController extends BasePluginController{
	@Resource
	private SqlService sqlService;
	@Resource
	private UserService userService;
	@Resource
	private SmsService smsService;
	


	/**
	 * 获取用于音视频调起的 UserSig
	 * @param token 当前用户的token
	 * @param otherToken 当前跟我聊天的对方的token
	 * @return 若成功，info返回后台登录成功的sessionid
	 */
	@RequestMapping(value="getUserSig.json", method = RequestMethod.POST)
	@ResponseBody
	public UserSigVO getUserSig(HttpServletRequest request,
			@RequestParam(value = "token", required = false , defaultValue="") String token,
			@RequestParam(value = "otherChatid", required = false , defaultValue="") String otherChatid){
		UserSigVO vo = new UserSigVO();
		if(token.length() < 32){
			vo.setBaseVO(UserSigVO.FAILURE, "token异常");
			return vo;
		}
		
		System.out.println("token:"+token);
		System.out.println("otherChatid:"+otherChatid);
		
		//根据自己token获取当前用户的信息
//		ChatUserBean chatUserBean = TokenUtil.getUser(token);
		String chatid = TokenUtil.getChatid(token);
		if(chatid == null){
			vo.setBaseVO(UserSigVO.FAILURE, "非法获取，token对应用户不存在");
			return vo;
		}
		
		//根据对方token取对方的信息
		ChatUserBean otherChatUserBean = UserUtil.getUser(otherChatid);
		if(otherChatUserBean == null){
			vo.setBaseVO(UserSigVO.FAILURE, "非法获取，token对应用户不存在！");
			return vo;
		}
		
		final String key = "extend:videoCall:tencent:usersig:{chatid}";  	//chatid便是当前这个人的chatid
		Object obj = CacheUtil.get(key.replace("{chatid}", chatid));
		if(obj == null){
			//缓存中没有，那么获取
			
			//过期时间12小时
			vo.setExpire(1*60*60*12);	
			vo.setUserid(Lang.uuid());
			TLSSigAPIv2 api = new TLSSigAPIv2(Global.SDKAPPID, Global.SECRETKEY);
			vo.setUsersig(api.genUserSig(vo.getUserid(), vo.getExpire()));
			
			CacheUtil.set(key.replace("{chatid}", chatid), vo, 1*60*60);	//数据缓存1小时
		}else{
			vo = (UserSigVO) obj;
		}
		
		//接口返回的vo
		UserSigVO resultVO = new UserSigVO();
		BeanUtils.copyProperties(vo, resultVO);
		
		ChatUserBean chatUserBean = UserUtil.getUser(chatid);
		
		//设置room，也就是组合两个用户的userid，将数字大的userid放前面
		if(chatUserBean.getUserid() > otherChatUserBean.getUserid()){
			resultVO.setRoomId(Lang.stringToLong(chatUserBean.getUserid()+""+otherChatUserBean.getUserid(), 0));
		}else{
			resultVO.setRoomId(Lang.stringToLong(otherChatUserBean.getUserid()+""+chatUserBean.getUserid(), 0));
		}
		resultVO.setSdkAppId(Global.SDKAPPID);
		
		ActionLogUtil.insertUpdateDatabase(request, "获取腾讯云音视频通话usersig", vo.toString());
		return resultVO;
	}
	

}
